<template>
  <div class="ele-body">
    <a-card :bordered="false">
      <!-- 搜索表单 -->
      <a-form :label-col="{ md: { span: 6 }, sm: { span: 24 } }" :wrapper-col="{ md: { span: 18 }, sm: { span: 24 } }">
        <a-row>
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="名称">
              <!-- <a-input v-model:value.trim="entryBillSonWhere.name" placeholder="请输入" @keyup.enter="entryBillSonReload" @change="entryBillSonReload" allow-clear />-->
              <a-input v-model:value.trim="entryBillSonWhere.name" placeholder="请输入名称" allow-clear />
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="选择">
              <a-select v-model:value="entryBillSonWhere.sex" placeholder="请选择" allow-clear>
                <a-select-option value="1">1</a-select-option>
                <a-select-option value="2">2</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item :wrapper-col="{ span: 24 }">
              <em></em>
              <a-space>
                <a-button type="primary" @click="entryBillSonReload">查询</a-button>
                <a-button @click="entryBillSonReset">重置</a-button>
              </a-space>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
      <!-- 表格 -->
      <ele-pro-table ref="entryBillSonTableRef" row-key="id" :columns="columns" :datasource="datasource" :scroll="{ x: 1000 }" cacheKey="entryBillSon">
        <template #toolbar>
          <a-space>
            <a-button type="primary" @click="entryBillSonOpenEdit()" v-any-permission="['store:entryBillSon:save']">
              <template #icon>
                <plus-outlined />
              </template>
              <span>添加</span>
            </a-button>
          </a-space>
        </template>
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'action'">
            <a-space>
              <a @click="entryBillSonOpenEdit(record)" v-any-permission="['store:entryBillSon:update']">修改</a>
              <a-divider type="vertical" />
              <a-popconfirm title="确定要删除此项吗？" @confirm="entryBillSonRemove(record)">
                <a class="ele-text-danger" v-any-permission="['store:entryBillSon:remove']">删除</a>
              </a-popconfirm>
            </a-space>
          </template>
        </template>
      </ele-pro-table>
    </a-card>
    <!-- 入库单子表添加、修改弹窗 -->
    <ele-modal :width="460" v-model:visible="entryBillSonVisible_modal" :confirm-loading="loading" :title="entryBillSonIsUpdate ? '修改入库单子表' : '添加入库单子表'"
      position="center" :body-style="{ paddingBottom: '8px' }" @ok="entryBillSonSave" :maskClosable="false" >
      <a-form ref="entryBillSonFormRef" :model="entryBillSonForm" :rules="entryBillSonRules" :label-col="{ md: { span: 7 }, sm: { span: 24 } }"
        :wrapper-col="{ md: { span: 17 }, sm: { span: 24 } }">
        <a-row :gutter="16">
          <a-col :md="24" :sm="24" :xs="24">
            <a-form-item label="we_entry_bill主表id" name="entrybillid">
              <a-input allow-clear :maxlength="200" placeholder="请输入we_entry_bill主表id" v-model:value="entryBillSonForm.entrybillid" autocomplete="off" />
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24" :xs="24">
            <a-form-item label="仓库" name="warehousename">
              <a-input allow-clear :maxlength="200" placeholder="请输入仓库" v-model:value="entryBillSonForm.warehousename" autocomplete="off" />
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24" :xs="24">
            <a-form-item label="产品" name="productname">
              <a-input allow-clear :maxlength="200" placeholder="请输入产品" v-model:value="entryBillSonForm.productname" autocomplete="off" />
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24" :xs="24">
            <a-form-item label="规格" name="specification">
              <a-input allow-clear :maxlength="200" placeholder="请输入规格" v-model:value="entryBillSonForm.specification" autocomplete="off" />
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24" :xs="24">
            <a-form-item label="长度" name="lengths">
              <a-input allow-clear :maxlength="200" placeholder="请输入长度" v-model:value="entryBillSonForm.lengths" autocomplete="off" />
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24" :xs="24">
            <a-form-item label="负差" name="negative">
              <a-input allow-clear :maxlength="200" placeholder="请输入负差" v-model:value="entryBillSonForm.negative" autocomplete="off" />
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24" :xs="24">
            <a-form-item label="数量" name="num">
              <a-input allow-clear :maxlength="200" placeholder="请输入数量" v-model:value="entryBillSonForm.num" autocomplete="off" />
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24" :xs="24">
            <a-form-item label="理算重量" name="liweight">
              <a-input allow-clear :maxlength="200" placeholder="请输入理算重量" v-model:value="entryBillSonForm.liweight" autocomplete="off" />
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24" :xs="24">
            <a-form-item label="理算总重量" name="litotalweight">
              <a-input allow-clear :maxlength="200" placeholder="请输入理算总重量" v-model:value="entryBillSonForm.litotalweight" autocomplete="off" />
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24" :xs="24">
            <a-form-item label="实际重量" name="actualweight">
              <a-input allow-clear :maxlength="200" placeholder="请输入实际重量" v-model:value="entryBillSonForm.actualweight" autocomplete="off" />
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24" :xs="24">
            <a-form-item label="实际总重量" name="actualtotalweight">
              <a-input allow-clear :maxlength="200" placeholder="请输入实际总重量" v-model:value="entryBillSonForm.actualtotalweight" autocomplete="off" />
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24" :xs="24">
            <a-form-item label="价格" name="price">
              <a-input allow-clear :maxlength="200" placeholder="请输入价格" v-model:value="entryBillSonForm.price" autocomplete="off" />
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24" :xs="24">
            <a-form-item label="总价" name="totalprice">
              <a-input allow-clear :maxlength="200" placeholder="请输入总价" v-model:value="entryBillSonForm.totalprice" autocomplete="off" />
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24" :xs="24">
            <a-form-item label="材质" name="model">
              <a-input allow-clear :maxlength="200" placeholder="请输入材质" v-model:value="entryBillSonForm.model" autocomplete="off" />
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24" :xs="24">
            <a-form-item label="产地" name="locality">
              <a-input allow-clear :maxlength="200" placeholder="请输入产地" v-model:value="entryBillSonForm.locality" autocomplete="off" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </ele-modal>
  </div>
</template>

<script setup>
import { ref, reactive, watch } from 'vue';
import { toDateString } from 'ele-admin-pro';
import useFormData from '@/utils/use-form-data';
import { message } from 'ant-design-vue';
import { TIME_OUT } from '@/config/damon';

import { pageEntryBillSon, removeEntryBillSon, addEntryBillSon, updateEntryBillSon } from '@/api/store/entryBillSon';

// 表格实例
const entryBillSonTableRef = ref(null);

// 表格列配置
const columns = ref([
  {
    key: 'index',
    width: 48,
    align: 'center',
    fixed: 'left',
    hideInSetting: true,
    customRender: ({ index }) => index + (entryBillSonTableRef.value?.tableIndex ?? 0)
  },
  {
    title: 'we_entry_bill主表id',
    dataIndex: 'entrybillid',
    key: 'entrybillid',
    // sorter: true
  },
  {
    title: '仓库',
    dataIndex: 'warehousename',
    key: 'warehousename',
    // sorter: true
  },
  {
    title: '产品',
    dataIndex: 'productname',
    key: 'productname',
    // sorter: true
  },
  {
    title: '规格',
    dataIndex: 'specification',
    key: 'specification',
    // sorter: true
  },
  {
    title: '长度',
    dataIndex: 'lengths',
    key: 'lengths',
    // sorter: true
  },
  {
    title: '负差',
    dataIndex: 'negative',
    key: 'negative',
    // sorter: true
  },
  {
    title: '数量',
    dataIndex: 'num',
    key: 'num',
    // sorter: true
  },
  {
    title: '理算重量',
    dataIndex: 'liweight',
    key: 'liweight',
    // sorter: true
  },
  {
    title: '理算总重量',
    dataIndex: 'litotalweight',
    key: 'litotalweight',
    // sorter: true
  },
  {
    title: '实际重量',
    dataIndex: 'actualweight',
    key: 'actualweight',
    // sorter: true
  },
  {
    title: '实际总重量',
    dataIndex: 'actualtotalweight',
    key: 'actualtotalweight',
    // sorter: true
  },
  {
    title: '价格',
    dataIndex: 'price',
    key: 'price',
    // sorter: true
  },
  {
    title: '总价',
    dataIndex: 'totalprice',
    key: 'totalprice',
    // sorter: true
  },
  {
    title: '材质',
    dataIndex: 'model',
    key: 'model',
    // sorter: true
  },
  {
    title: '产地',
    dataIndex: 'locality',
    key: 'locality',
    // sorter: true
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    key: 'createTime',
    resizable: true,
    width: 150,
    ellipsis: true,
    customRender: ({ text }) => toDateString(text)
  },
  {
    title: '创建人',
    dataIndex: 'createName',
    key: 'createName',
  },
  {
    title: '更新时间',
    dataIndex: 'updateTime',
    key: 'updateTime',
    resizable: true,
    width: 150,
    ellipsis: true,
    customRender: ({ text }) => toDateString(text)
  },
  {
    title: '更新人',
    dataIndex: 'updateName',
    key: 'updateName',
  },
  {
    title: '操作',
    key: 'action',
    width: 200,
    align: 'center',
    fixed: 'right',
    hideInSetting: true
  }
]);

// 表单数据
const { form: entryBillSonWhere, resetFields } = useFormData({
  username: '',
  sex: undefined
});

// 表格数据源
const datasource = ({ page, limit, where, orders }) => {
  return pageEntryBillSon({ ...where, ...orders, page, limit });
};

/* 搜索 */
const entryBillSonReload = () => {
  entryBillSonTableRef?.value?.reload({ page: 1, where: entryBillSonWhere });
};
const entryBillSonReload2 = () => {
  entryBillSonTableRef?.value?.reload();
};

let entryBillSonWhere_timeout = 0
watch(() => entryBillSonWhere, (val) => {
  clearTimeout(entryBillSonWhere_timeout)
  entryBillSonWhere_timeout = setTimeout(() => {
    entryBillSonReload();
  }, TIME_OUT);
},
{ deep: true }
)

/*  重置 */
const entryBillSonReset = () => {
  resetFields();
  entryBillSonReload();
};

/* 删除单个 */
const entryBillSonRemove = (record) => {
  const hide = message.loading('请求中..', 0);
  removeEntryBillSon(record.id).then((msg) => {
    hide();
    message.success(msg);
    entryBillSonReload2();
  }).catch((e) => {
    hide();
    message.error(e.message);
  });
};

// 当前选中数据
const entryBillSonCurrent = ref(null);

// 弹窗是否打开
const entryBillSonVisible_modal = ref(false);

// 是否是修改
const entryBillSonIsUpdate = ref(false);

// 提交状态
const loading = ref(false);

//
const entryBillSonFormRef = ref(null);

// 表单数据
const { form: entryBillSonForm, resetFields: entryBillSonResetFormFields } = useFormData({
  id: undefined,
  customer: '',
  vessel: '',
});

// 表单验证规则
const entryBillSonRules = reactive({
  name: [
    {
      required: true,
      message: '请输入名称',
      type: 'string',
    }
  ],
});

/* 打开编辑弹窗 */
const entryBillSonOpenEdit = (record) => {
  entryBillSonResetFormFields();
  entryBillSonFormRef.value?.clearValidate();
  entryBillSonCurrent.value = record?? {};
  entryBillSonVisible_modal.value = true;
  Object.assign(entryBillSonForm, record ?? {});
  entryBillSonIsUpdate.value = !!record;
};

/* 保存编辑 */
const entryBillSonSave = () => {
  if (!entryBillSonFormRef.value) {
    return;
  }
  entryBillSonFormRef.value.validate().then(() => {
    loading.value = true;
    const saveOrUpdate = entryBillSonIsUpdate.value ? updateEntryBillSon : addEntryBillSon;
    saveOrUpdate(entryBillSonForm).then((msg) => {
      loading.value = false;
      message.success(msg);
      entryBillSonVisible_modal.value = false;
      entryBillSonReload2();
    }).catch((e) => {
      loading.value = false;
      message.error(e.message);
    });
  }).catch(() => {
  });
};
</script>

<script>
export default {
  name: 'EntryBillSon'
};
</script>

